home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_2.lha / 6_2 / 6_2a.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  676b  |  32 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / substring operator
  6. / exercise 6.2
  7. tring string::operator()(int index, int count)
  8.  
  9.    char *s1 = p->s;
  10.    int s1len = strlen(s1);
  11.  
  12.    // convert left index, if necessary
  13.    if (index < 0)
  14. index += s1len;
  15.  
  16.    // left index past end of string
  17.    else if (index >= s1len)
  18. return "";
  19.  
  20.    // convert count, if necessary
  21.    int numleft = s1len - index;
  22.    if (count > numleft || count < 0)
  23. count = numleft;
  24.  
  25.    // copy the substring
  26.    char *s2 = new char[count + 1];
  27.    strncpy(s2, s1+index, count);
  28.    s2[count] = '\0';
  29.    return s2;
  30.  
  31.  
  32.